#include "global.h"
#include "GLRender.h"


#include "com_vc_core_VideoController.h"

jint
Java_com_vc_core_VideoController_request
  (JNIEnv *env, jobject clazz, jlong toAccount)
{
	return 0;
}

jint
Java_com_vc_core_VideoController_accept
  (JNIEnv *env, jobject clazz, jlong toAccount)
{
	return 0;
}

jint
Java_com_vc_core_VideoController_cancelRequest
  (JNIEnv *env, jobject clazz, jlong toAccount)
{
	return 0;
}

jint
Java_com_vc_core_VideoController_close
  (JNIEnv *env, jobject clazz, jlong toAccount)
{
	return 0;
}

void
Java_com_vc_core_VideoController_setPeerGLRender
  (JNIEnv *env, jobject clazz, jobject glRender)
{
	return;
}

jint
Java_com_vc_core_VideoController_sendVideoData
  (JNIEnv *env, jobject clazz, jbyteArray data, jlong toAccount, jint width, jint height, jint format, jint fps)
{
	jbyte *buf = (jbyte *)(*env)->GetByteArrayElements(env, data, 0);
	if (!buf)
	{
		LOGE("GetByteArrayElements fail\n");
		return -1;
	}
	
	int ret = 0;
	
	PROC_BEGIN
	if (g_render_cls == 0)
	{
		LOGE("g_render_cls is null\n");
		ret = -2;
		break;
	}
	
	if (g_render_mid == 0)
	{
		LOGE("g_render_mid is null\n");
		ret = -3;
		break;
	}
	
	if (g_render_obj == 0)
	{
		LOGE("g_render_obj is null\n");
		ret = -4;
		break;
	}
	
	jsize buf_size = (*env)->GetArrayLength(env, data);
	if (!(buf_size > 0))
	{
		LOGE("buf_size:%d\n", buf_size);
		ret = -5;
		break;
	}

	if (g_vedio_buffer_size != (size_t)buf_size)
	{
		g_vedio_buffer = realloc(g_vedio_buffer, buf_size);
		g_vedio_buffer_size = buf_size;
	}
	else
	{
		if (!g_vedio_buffer)
		{
			g_vedio_buffer = (uint8_t *)malloc(buf_size);
		}
	}

	memcpy(g_vedio_buffer, buf, buf_size);
	memcpy(g_vedio_buffer+width*height, buf+width*height+width*height/4, width*height/4);
	memcpy(g_vedio_buffer+width*height+width*height/4, buf+width*height, width*height/4);

	g_width = width;
	g_height = height;
	g_format = format;
	g_fps = fps;
	
	FrameBuffer fb;
	fb.mpData = g_vedio_buffer;
	fb.mAngle = 0;
	fb.mType = def_DataType_YUV420P;
	fb.mfMirror = false;

	setFrame(&fb);

	(*env)->CallStaticVoidMethod(env, g_render_cls, g_render_mid, g_render_obj, 0, g_render_obj);
	PROC_END

	(*env)->ReleaseByteArrayElements(env, data, buf, 0);
	return ret;
}

jint
Java_com_vc_core_VideoController_PauseVideo
  (JNIEnv *env, jobject clazz, jlong toAccount)
{
	return 0;
}

jint
Java_com_vc_core_VideoController_ResumeVideo
  (JNIEnv *env, jobject clazz, jlong toAccount)
{
	return 0;
}

jint
Java_com_vc_core_VideoController_AudioInData
  (JNIEnv *env, jobject clazz, jbyteArray recData, jint len)
{
	return 0;
	jbyte *buf = (jbyte *)(*env)->GetByteArrayElements(env, recData, 0);
	if (!buf)
	{
		return -1;
	}
	jsize buf_size = (*env)->GetArrayLength(env, recData);
	if (!(buf_size > 0))
	{
		return -2;
	}
	
	if (!g_audio_buffer)
	{
		g_audio_buffer = (uint8_t *)malloc(buf_size);
		g_audio_buffer_size = buf_size;
	}
	else if (g_audio_buffer_size != buf_size)
	{
		g_audio_buffer = realloc(g_audio_buffer, buf_size);
		g_audio_buffer_size = buf_size;
	}
	memcpy(g_audio_buffer, buf, buf_size);
	
	/*jclass cls = (*env)->GetObjectClass(env, clazz);
	jmethodID method = (*env)->GetMethodID(env, cls, "playAudioData","([BI)I");
	jbyteArray playData = (*env)->NewByteArray(env, buf_size);
	(*env)->CallIntMethod(env, clazz, method, playData, buf_size);*/
	(*env)->ReleaseByteArrayElements(env, recData, buf, 0);
	return 0;
}

jint 
Java_com_vc_core_VideoController_AudioOutData
  (JNIEnv *env, jobject clazz, jbyteArray playData, jint len)
{
	jbyte *buf = (jbyte *)(*env)->GetByteArrayElements(env, playData, 0);
	if (!buf)
	{
		len = 0;
		return -1;
	}
	
	int ret = 0;
	
	PROC_BEGIN
	jsize buf_size = (*env)->GetArrayLength(env, playData);
	if (!(buf_size > 0))
	{
		len = 0;
		ret = -2;
		break;
	}
	
	if ((size_t)buf_size > (size_t)g_audio_buffer_size)
	{
		buf_size = g_audio_buffer_size;
	}
	if ((size_t)buf_size > (size_t)len)
	{
		buf_size = len;
	}
	len = buf_size;
	(*env)->SetByteArrayRegion(env, playData, 0, buf_size, g_audio_buffer);
	PROC_END
	
	(*env)->ReleaseByteArrayElements(env, playData, buf, 0);
	g_audio_buffer_size = 0;
	return ret;
}

jint 
Java_com_vc_core_VideoController_SetAudioInputMute
  (JNIEnv *env, jobject clazz, jlong account, jboolean enable)
{
	return 0;
}

jint 
Java_com_vc_core_VideoController_SetAudioOutputMute
  (JNIEnv *env, jobject clazz, jlong account, jboolean enable)
{
	return 0;
}

jint 
Java_com_vc_core_VideoController_SetAudioSpeakerVolume
  (JNIEnv *env, jobject clazz, jint value)
{
	return 0;
}

jint 
Java_com_vc_core_VideoController_GetAudioDeviceParam
  (JNIEnv *env, jobject clazz, jintArray mFormat, jintArray mSamplerate, jintArray mFrameLen, jintArray mWorkmode)
{
	return 0;
}
